package net.admin.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.admin.action.Action;
import net.admin.action.ActionForward;
import net.admin.db.item.CategoryDAO;

public class CategoryRegistAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ActionForward af = new ActionForward();
		af.setRedirect(false);
		CategoryDAO dao = null;
		boolean result=false;
		af.setPath("/admin/item/category_reg.jsp");
		
		
		HttpSession session = request.getSession();
		String admin_id=(String)session.getAttribute("user_id");
		String category_nm = request.getParameter("category_nm");
		String category_cd = null;
		try{
			dao = new CategoryDAO();
			result=dao.registCategory(admin_id, category_nm);
			if(result){
				category_cd=dao.getCategoryCode(category_nm);
				request.setAttribute("category_cd", category_cd);
			}
			request.setAttribute("category_nm", category_nm);
			request.setAttribute("result", result?"Y":"N");
		}catch(Exception e){
			request.setAttribute("result", "N");
			e.printStackTrace();
		}finally{
			dao.close();
		}
		
		return af;
	}

}
